<?php
/**
 * Created by IntelliJ IDEA.
 * User: ahwwl
 * Date: 18-1-3
 * Time: 下午3:42
 */

namespace app\controller;


use app\common\jssdk;
use app\common\weixin;
use app\model\CardList;
use app\model\CardPackageList;
use app\model\OrderList;
use app\model\RechargeCardList;
use commen\ebjyd;
use think\Request;

/**
 * @property CardList CardList
 * @property CardPackageList CardPackageList
 * @property OrderList OrderList
 * @property RechargeCardList RechargeCardList
 */
class card extends CommonController
{
    function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->CardList = new CardList();
        $this->CardPackageList = new CardPackageList();
        $this->OrderList = new OrderList();
        $this->RechargeCardList = new RechargeCardList();

    }

    function card_list()
    {
        $card_package_list = $this->CardPackageList
            ->where(["status"=>0])
            ->order("id desc")
            ->select();
        $this->assign("card_package_list",$card_package_list);
        $this->assign("page_title","加油卡列表");
        return $this->fetch();
    }
    function ajax_card_list()
    {

        try
        {
            $page = input("page",1,"intval");
            $rows = input("rows",30,"intval");
            $card_no = input("card_no","","trim");
            $status = input("status",0,"intval");

            $where = [];
            if(!empty($card_no))
                $where["card_no"] = ["like","%".$card_no."%"];
            if($status !=-1)
                $where["status"] = $status;

            $count = $this->CardList
                ->where($where)
                ->count();
            $page_total = ceil($count/$rows);
            $page = $page>$page_total?$page_total:$page;
            $page = $page < 1? 1:$page;
            $order_log = $this->CardList
                ->where($where)
                ->order("id desc")
                ->limit(($page-1)*$rows,$rows)
                ->select();


            $this->resultdata["data"] = $order_log;
            $this->resultdata["error"] = 0;
            $this->resultdata["total"] = $count;
            $this->resultdata["page"] = $page;
            $this->resultdata["rows"] = $rows;
        }catch (\Exception $e)
        {

            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);

    }
    function ajax_card_info()
    {
        try
        {
            $id = input("id",0,"intval");
            if(empty($id))
                $this->E("参数不能为空");
            $info = $this->CardList->where(["id"=>$id])->find();
            if(!empty($info))
                $info["balance"] = number_format($info["balance"]/10000, 2, '.', '');
            $this->resultdata["data"] = $info;
            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_card_card_balance()
    {
        try
        {
            $card_no = input("card_no",0,"intval");
            if(empty($card_no))
                $this->E("参数不能为空");
            $info = $this->CardList->where(["card_no"=>$card_no])->find();
            if(empty($info))
                $this->E("卡号不存在");
            $result = ebjyd::get_card($card_no);
            if(!empty($result) &&$result["error"] == 0)
            {
                if(isset($result["data"]["card_balance"])&&isset($result["data"]["pre_balance"]))
                {
                    $card_balance = !empty($result["data"]["card_balance"])?intval($result["data"]["card_balance"])*100:0;
                    $pre_balance = !empty($result["data"]["pre_balance"])?intval($result["data"]["pre_balance"])*100:0;
                    $update = [
                        "card_balance" => $card_balance,
                        "pre_balance"  => $pre_balance,
                        "mtime"        => NOW_TIME
                    ];
                    $this->CardList->where(["card_no"=>$card_no])->update($update);
                }
                $this->resultdata["data"] = $result["data"];
            }else{
                exception("查询失败");
            }

            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_card_info_save()
    {
        try
        {
            $id = input("id",0,"intval");
            $card_no = input("card_no","","trim");
            $status = input("status",0,"intval");
            $card_type = input("card_type",0,"intval");
            $balance = input("balance",0,"intval");
            if(!in_array($status,[0,1,2])||empty($card_no))
                $this->E("参数不正确");
            $balance = $balance*10000;
            $info = [
                "card_no"=>$card_no,
                "card_type"=>$card_type,
                "balance" =>$balance,
                "status"=>$status,
                "ctime"=>NOW_TIME
            ];
            if($id)
            {
                $query = $this->CardList->where(["id"=>$id])->update($info);
                if(empty($query))
                {
                    $this->E("更新失败");
                }
            }else{
                $id = $this->CardList->insertGetId($info);
                if(empty($id))
                {
                    $this->E("添加失败");
                }
            }
            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_card_list_save()
    {
        try
        {
            $card_list = input("card_list","","trim");
            $status = input("status",0,"intval");
            $balance = input("balance",0,"intval");
            $card_type = input("card_type",0,"intval");
            if(!in_array($status,[0,1,2])||empty($card_list))
                $this->E("参数不正确");
            $balance = $balance*10000;
            $list = [];
            $card_list = explode("\n",$card_list);
            if(empty($card_list))
                $this->E("参数不正确");
            foreach ($card_list as $card_no)
            {
                $card_no = trim($card_no);
                if(!empty($card_no))
                {
                    $info = [
                        "card_no"=>$card_no,
                        "card_type"=>$card_type,
                        "balance" =>$balance,
                        "status"=>$status,
                        "ctime"=>NOW_TIME
                    ];
                    $list[] = $info;
                }

            }
            if(empty($list))
                $this->E("卡列表为空");
            $query = $this->CardList->insertAll($list);
            if(empty($query))
            {
                $this->E("添加失败");
            }
            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_add_balance()
    {
        try
        {
            $id = input("id",0,"intval");
            $product_id = input("product_id",0,"intval");

            if(empty($id)||empty($product_id))
                $this->E("参数不正确");

            $card_info = $this->CardList->where(["id"=>$id])->find();
            if(empty($card_info) ||empty($card_info["card_no"]))
            {
                $this->E("卡不存在");
            }
            if($card_info["status"]!=0)
            {
                $this->E("只允许正常的卡充值预存款");
            }
            $card_no = $card_info["card_no"];
            $card_type = $card_info["card_type"];
            $card_package_info = $this->CardPackageList
                ->where(["status"=>0,"id"=>$product_id,"mode"=>0])
                ->order("id desc")
                ->find();
            if(empty($card_package_info))
                $this->E("套餐不存在");
            $goods_id = $card_package_info["goods_id"];
            $basic_price = $card_package_info["basic_price"];
            $cost = $card_package_info["cost"];
            $money = $card_package_info["cost"];
            $mode = $card_package_info["mode"];
            $order_no = get_order_no("D");
            $total_money = $money;
            $info = [
                "order_no" => $order_no,
                "card_no" =>$card_no,
                "card_type" =>$card_type,
                "product_id" => $product_id,
                "goods_id" =>$goods_id,
                "basic_price"=>$basic_price,
                "cost" => $cost,
                "money" => $money,
                "discount_money"=>0,
                "total_money" =>$total_money,
                "mode" =>$mode,
                "pay_type"=>3,
                "pay_status"=>1,
                "ctime" => NOW_TIME,
                "mtime" => NOW_TIME,
                "ip" => $this->request->ip()
            ];
            $query = $this->OrderList->insert($info);
            if(empty($query))
                $this->E("添加订单失败");
            $this->order_log($order_no,"添加预存款，卡号：".$card_no);
            $this->order_log($order_no,"提交充值");
//            if(!sendQueue("app\job\Orders::order_recharge",[$order_no]))
//            {
//                exception("提交充值订单失败");
//            }
            if(!sendMQQueue("fuel_order_recharge",$order_no))
            {
                exception("提交充值订单失败");
            }
            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function card_package_list()
    {
        $this->assign("page_title","充值套餐列表");
        return $this->fetch();
    }
    function ajax_card_package_list()
    {

        try
        {
            $page = input("page",1,"intval");
            $rows = input("rows",30,"intval");
            $name = input("name","","trim");

            $where = [];
            if(!empty($name))
                $where["card_no"] = ["like",$name."%"];

            $count = $this->CardPackageList
                ->where($where)
                ->count();
            $page_total = ceil($count/$rows);
            $page = $page>$page_total?$page_total:$page;
            $page = $page < 1? 1:$page;
            $order_log = $this->CardPackageList
                ->where($where)
                ->order("id desc")
                ->limit(($page-1)*$rows,$rows)
                ->select();


            $this->resultdata["data"] = $order_log;
            $this->resultdata["error"] = 0;
            $this->resultdata["total"] = $count;
            $this->resultdata["page"] = $page;
            $this->resultdata["rows"] = $rows;
        }catch (\Exception $e)
        {

            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);

    }
    function ajax_card_package_info()
    {
        try
        {
            $id = input("id",0,"intval");
            if(empty($id))
                $this->E("参数不能为空");
            $info = $this->CardPackageList->where(["id"=>$id])->find();
            $info["basic_price"]=!empty($info["basic_price"])?intval($info["basic_price"]/10000):intval($info["basic_price"]);
            $info["cost"]=!empty($info["cost"])?intval($info["cost"]/10000):intval($info["cost"]);
            $info["money"]=!empty($info["money"])?intval($info["money"]/10000):intval($info["money"]);

            $this->resultdata["data"] = $info;
            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_card_package_info_save()
    {
        try
        {
            $id = input("id",0,"intval");
            $name = input("name","","trim");
            $mode = input("mode",0,"intval");
            $goods_id = input("goods_id",0,"intval");
            $card_type = input("card_type",1,"intval");
            $basic_price = input("basic_price",0,"floatval");
            $cost = input("cost",0,"floatval");
            $money = input("money",0,"floatval");
            $status = input("status",0,"intval");

            if(!in_array($status,[0,1])||!in_array($mode,[0,1,2])||!in_array($card_type,[0,1,2])||empty($name) ||empty($basic_price)||empty($cost)||empty($money))
                $this->E("参数不正确");
            $basic_price = $basic_price*10000;
            $cost = $cost*10000;
            $money = $money*10000;
            $info = [
                "name"=>$name,
                "mode"=>$mode,
                "card_type"=>$card_type,
                "goods_id"=>$goods_id,
                "basic_price"=>$basic_price,
                "cost"=>$cost,
                "money"=>$money,
                "status"=>$status
            ];
            if($id)
            {
                $query = $this->CardPackageList->where(["id"=>$id])->update($info);
                if(empty($query))
                {
                    $this->E("更新失败");
                }
            }else{
                $id = $this->CardPackageList->insertGetId($info);
                if(empty($id))
                {
                    $this->E("添加失败");
                }
            }
            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_card_package_del()
    {
        try {
            $row_list = input("row_list", []);

            if (empty($row_list) ) {
                $this->E("参数不正确");
            }
            $id_list = make_array($row_list,"id");
            if (empty($id_list) ) {
                $this->E("参数不正确");
            }


            $query = $this->CardPackageList->where(['id' => ["in",$id_list]])->delete();

            if ($query)
                $this->resultdata["error"] = 0;
            else
                $this->resultdata["msg"] = "删除失败";
        } catch (\Exception $e) {

            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function recharge_card_list()
    {
        $weixin = new weixin();
        $w_config = $weixin->getSignPackage();
//        $w_config2 = $weixin->getSignPackage(2);
        $this->assign("w_config",$w_config);
//        $this->assign("w_config2",$w_config2);
        $this->assign("page_title","充值卡列表");
        return $this->fetch();
    }
    function ajax_recharge_card_list()
    {
        try
        {
            $page = input("page",1,"intval");
            $rows = input("rows",30,"intval");
            $card_pwd = input("card_pwd","","trim");

            $where = [];
            if(!empty($card_pwd))
                $where["$card_pwd"] = ["like","%".$card_pwd."%"];

            $count = $this->RechargeCardList
                ->where($where)
                ->count();
            $page_total = ceil($count/$rows);
            $page = $page>$page_total?$page_total:$page;
            $page = $page < 1? 1:$page;
            $order_log = $this->RechargeCardList
                ->where($where)
                ->order("id desc")
                ->limit(($page-1)*$rows,$rows)
                ->select();
            foreach ($order_log as $key=>$val)
            {
                $length = strlen($val["card_pwd"])-10;
                $order_log[$key]["card_pwd_text"] = substr_replace($val["card_pwd"],"****","5",$length);

            }


            $this->resultdata["data"] = $order_log;
            $this->resultdata["error"] = 0;
            $this->resultdata["total"] = $count;
            $this->resultdata["page"] = $page;
            $this->resultdata["rows"] = $rows;
        }catch (\Exception $e)
        {

            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_recharge_card_save()
    {
        try
        {
            $card_pwd = input("card_pwd","","trim");
            $money = input("money",0,"intval");

            if(empty($card_pwd)||empty($money))
                exception("参数不正确");

            $money = $money*10000;
            $info = [
                "card_pwd"=>$card_pwd,
                "money"=>$money,
                "status"=>0,
                "ctime" => NOW_TIME,
                "mtime" => NOW_TIME
            ];
            $count = $this->RechargeCardList->where(["card_pwd"=>$card_pwd])->count();
            if($count>0)
                exception("充值卡密码已经存在");
            $id = $this->RechargeCardList->insertGetId($info);
            if(empty($id))
            {
                exception("添加失败");
            }
            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
}